//
//  SlideTabBar.m
//  Douyin
//
//  Created by Qiao Shi on 2018/10/22.
//  Copyright © 2018 Qiao Shi. All rights reserved.
//

#import "SlideTabBar.h"

@interface SlideTabBar ()

@property (nonatomic, strong) UIView                           *slideLightView;
@property (nonatomic, strong) NSMutableArray<UILabel *>        *labels;
@property (nonatomic, strong) NSMutableArray<NSString *>       *titles;
@property (nonatomic, assign) NSInteger                        tabIndex;
@property (nonatomic, assign) CGFloat                          itemWidth;
@property (nonatomic, strong) UIView *topLine;

@end

@implementation SlideTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self){
        _labels = [NSMutableArray array];
        _titles = [NSMutableArray array];
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    if(_titles.count == 0) {
        return;
    }
    
    [[self subviews] enumerateObjectsUsingBlock:^(UIView *subView, NSUInteger idx, BOOL *stop) {
        [subView removeFromSuperview];
    }];
    [_labels removeAllObjects];
    
    CGFloat itemWidth = _itemWidth = KScreenWidth/_titles.count;
    [_titles enumerateObjectsUsingBlock:^(NSString * title, NSUInteger idx, BOOL *stop) {
        UIView *container = [[UIView alloc]init];
        container.userInteractionEnabled = YES;
        container.tag = idx;
        [container addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapAction:)]];
        container.frame = CGRectMake(idx*itemWidth, 0, itemWidth, self.bounds.size.height);
        [self addSubview:container];
        UILabel *label = [[UILabel alloc]init];
        label.text = title;
        label.textColor = UICOLOR_WITH_RGBINT_A(0xffffff, 0.6);
//        label.textAlignment = NSTextAlignmentCenter;
        label.font = FONT(customFontName, 17);
        
//        label.userInteractionEnabled = YES;
//        [label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapAction:)]];
        [container addSubview:label];
        [self.labels addObject:label];
        [container addSubview:label];
        label.frame = CGRectMake(15, 0, itemWidth - 15, self.bounds.size.height);
//        label.frame = CGRectMake(idx*itemWidth, 0, itemWidth, self.bounds.size.height);
//        if(idx != self.titles.count - 1) {
//            UIView *spliteLine = [[UIView alloc] initWithFrame:CGRectMake((idx+1)*itemWidth - 0.25f, 12.5f, 0.5f, self.bounds.size.height - 25.0f)];
//            spliteLine.backgroundColor = UICOLOR_WITH_RGBINT_A(0xffffff, 0.2);
//            spliteLine.layer.zPosition = 10;
//            [self addSubview:spliteLine];
//        }
    }];
    _labels[_tabIndex].textColor = [UIColor whiteColor];
    [self addSubview:self.topLine];
    [_topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).mas_offset(2);
        make.left.and.right.mas_equalTo(self).mas_offset(0);
        make.height.mas_equalTo(0.5);
    }];
    self.slideLightView.frame = CGRectMake(_tabIndex * itemWidth + 15, 1, 20, 4);
    [self addSubview:self.slideLightView];
//    _slideLightView = [[UIView alloc] init];
//    _slideLightView.backgroundColor = [UIColor redColor];
//    _slideLightView.frame = CGRectMake(_tabIndex * itemWidth + 15, self.bounds.size.height-2, itemWidth - 30, 2);
//    [self addSubview:_slideLightView];
}

- (void)setLabels:(NSArray<NSString *> *)titles tabIndex:(NSInteger)tabIndex {
    [_titles removeAllObjects];
    [_titles addObjectsFromArray:titles];
    _tabIndex = tabIndex;
//    [self setNeedsLayout];
//    [self layoutIfNeeded];
    for (NSInteger i=0; i<self.labels.count;i++) {
        UILabel *label = [self.labels objectAtIndex:i];
        label.text = [titles objectAtIndex:i];
    }
    CGRect frame = self.slideLightView.frame;
    frame.origin.x = self.itemWidth * _tabIndex + 15;
    [self.slideLightView setFrame:frame];
}

- (void)onTapAction:(UITapGestureRecognizer *)sender {
    NSInteger index = sender.view.tag;
    _tabIndex = index;
    if(_delegate) {
        [UIView animateWithDuration:0.10
                              delay:0
             usingSpringWithDamping:0.8
              initialSpringVelocity:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             CGRect frame = self.slideLightView.frame;
                             frame.origin.x = self.itemWidth * index + 15;
                             [self.slideLightView setFrame:frame];
                             [self.labels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
                                 label.textColor = index == idx ? [UIColor whiteColor] : UICOLOR_WITH_RGBINT_A(0xffffff, 0.6);
                             }];
                         } completion:^(BOOL finished) {
                             [self.delegate onTabTapAction:index];
                         }];
        
    }
}

-(UIView *)topLine{
    if(!_topLine){
        _topLine = [[UIView alloc]init];
        _topLine.backgroundColor = UICOLOR_WITH_RGBINT_A(0xffffff, 0.2);
    }
    return _topLine;
}

-(UIView *)slideLightView{
    if(!_slideLightView){
        _slideLightView = [[UIView alloc] init];
        _slideLightView.backgroundColor = [UIColor redColor];
        ViewRadius(_slideLightView, 2);
//        _slideLightView.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0.5);
//        _slideLightView.frame = CGRectMake(_tabIndex * itemWidth + 15, self.bounds.size.height-2, itemWidth - 30, 2);
    }
    return _slideLightView;
}

@end
